import {ajax,getUserInfo} from '@/common/common.js'
import {bpmUrl} from "@/common/config.js"
//初始赋值
function  formonload(list,formData,bussiness){
	initListOption(list,'OldDepart')
	let currentNodeid = bussiness.currentNodeid
	let arr = list.map(item => {
		if(item.name == 'cCusCode'){
			item.disabled = true
		}
		if(formData.cCusName){
			console.log('12344544')
			if(['cCusCode','Department','cCusDefine6','cCusFax','cCusPhone','cCusHand','Cust_TYPE','cCusDefine7','cCusDefine2'].indexOf(item.name) != -1){
				item.disabled = true
			}
		}
		if(currentNodeid=="T10002"||currentNodeid=="T10018"||currentNodeid=="T10004"||currentNodeid=="T10023"||currentNodeid=="T10019"||currentNodeid=="T10005"||currentNodeid=="T10006"){
				if(['license','SQLX'].indexOf(item.name) != -1){
		    	item.hide = '1' //隐藏
		    }
			}else{
				if(['license','SQLX'].indexOf(item.name) != -1){
					item.hide = '0' //显示
				}
			}
			return item
	})
	return arr
}
// 加载大区下拉数据
function initListOption(list,name){
	let user = getUserInfo()
	let paramas = {
		paramasMap: {},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/getArea'
	}
		ajax({
			url:'/bpm/get',
			method: 'POST',
			data: paramas,
			success:res => {
				let arr = res.data.data
				arr = arr.map(item => {
					item.label = item.text
					return item
				})
				list.map(item => {
					if(item.name == name){
						item.optiona.data = arr
					}
				})
			}
		})
}
//给相关字段赋值
function fz(obj,item,formData,list){
	console.log('打印',obj,item,formData,list)
	formData.cCusName = obj.cCusName
	formData.cCusCode = obj.cCusCode
	formData.comCode1 = obj.comCode1
	let user = getUserInfo()
	let params = {
		paramasMap: {
			cCusCode: obj.cCusCode,
			comCode: ""
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/getDepartmentByCondition'
	}
	// // 省份
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.Department = res.data.data.Department
		}
	})
	// 客户类别
	params.url = bpmUrl + '/rest/appform/getcCusDefine6'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusDefine6 = res.data.data.cCusDefine6
		}
	})
	//传真号码(cCusFax)
	params.url = bpmUrl + '/rest/appform/getcCusFax'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusFax = res.data.data.cCusFax
		}
	})
	//电话(cCusPhone)
	params.url = bpmUrl + '/rest/appform/getcCusPhone'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusPhone = res.data.data.cCusPhone
		}
	})
	//手机(cCusHand)
	params.url = bpmUrl + '/rest/appform/getcCusHand'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusHand = res.data.data.cCusHand
		}
	})
	//客户类型(Cust_TYPE)
	// params.url = bpmUrl + '/rest/appform/getcCusHand'
	// ajax({
	// 	url:'/bpm/get',
	// 	method: 'POST',
	// 	data: params,
	// 	success:res => {
	// 		formData.cCusHand = res.data.data.cCusHand
	// 	}
	// })
	formData.Cust_TYPE = '客户类型'
	//开票类型(cCusDefine2)
	params.url = bpmUrl + '/rest/appform/getcCusDefine2'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusDefine2 = res.data.data.cCusDefine2
		}
	})
	//合作情况(ccdefine7)
	params.url = bpmUrl + '/rest/appform/getccdefine7'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.ccdefine7 = res.data.data.ccdefine7
		}
	})
	list.map(item => {
		if(['comCode1','Department','cCusDefine6','cCusFax','cCusPhone','cCusHand','Cust_TYPE','cCusDefine2','ccdefine7'].indexOf(item.name) != -1){
			item.disabled = true // 设置字段disabled
		}
		return item
	})
}
function formonsubmit(formData,list,bussiness,subforms){
	let currentNodeid = bussiness.currentNodeid
	if(currentNodeid == "T10002"){
		//授权公司名称为必填项
		var comCode = formData.comCode;
		if(comCode=="0"){
			uni.showToast({
				title: "【请选择【授权公司名称】！】",
				icon:'none'
			})
			return false;
		}
		//判断授权客户+产品列表不能为空
		if(subforms.length == 0){
			uni.showToast({
				title: "【请填写表体信息！】",
					icon:'none'
			})
			return false;
		}
		//验证产品和授权有效期不能为空
		// alert("【请填写【授权客户+产品列表】中的产品信息！】");
		// alert("【请填写【授权客户+产品列表】中的授权有效期！】");
		
	}
	list = list.map(item => {
		if(['cCusCode','comCode1','Department','cCusDefine6','cCusFax','cCusPhone','cCusHand','Cust_TYPE','cCusDefine7','cCusDefine2','ccdefine5','ccdefine6','ccdefine7'].indexOf(item.name) != -1){
			item.disabled = false
		}
		return item
	})
	return true;
}
//模糊搜索客户列表
function getCustomerName(queryString, formData, cb) {
	console.log(queryString, cb)
	let user = getUserInfo()
	let paramas = {
		paramasMap: {
			cCusName: queryString
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/getCustomerInfo'
	}
	ajax({
		url: '/bpm/get',
		method: 'POST',
		data: paramas,
		success: res => {
			console.log(res)
			let data = res.data.data.map(item => {
				item.id = item.cCusCode
				item.name = item.cCusName
				return item
			})
			console.log(data)
			cb(data)
		}
	})
}

export default {
	formonload,
	fz,
	formonsubmit,
	getCustomerName
}